home *** CD-ROM | disk | FTP | other *** search
RISC OS BBC BASIC V Source | 1994-02-20 | 3.3 KB | 143 lines |
- >HeapTest
- Globals for test program
- quit% =
- a% = 0
- b% = 0
- blk% = 0
- amount$ = ""
- g = 0
- A simple menu to test the heap procedures
- "Select from..."
- "1= fetch"'"2= return"'"3= resize"'"4= quit"
- g =
- - 48
-
- "size to fetch: &"a$
-
- a$ <> ""
- a% =
- ("&" + a$)
- ! blk% =
- _heap_get(a%)
-
- blk% > 0
- 0
- '"Block allocated at &" +
- ~blk%
-
- 0
- '"Unable to claim enough memory"
-
-
- "
- "block to return: &"a$
-
- a$ <> ""
- a% =
- ("&" + a$)
- b% = a%
-
- _heap_release(a%)
-
- (a% = 0)
- #;
- '"Block at &" +
- ~b% + " has been released"
-
- %,
- '"No block exists at &"+
-
-
- )"
- "Block to resize: &"a$
- *0
- "Amount (-ve to decrease): &"amount$
-
- a$ <> ""
- a% =
- ("&" + a$)
- b% = a%
- .(
- _heap_resize(a%,
- (amount$))
-
- (a% > 0)
- 0<
- '"Block at &" +
- ~b% + " has been resized ";
- 15
- a% <> b%
- "(now at &"+
- ~a%+")"
-
- 38
- a% = -3
- '"No block exists at &"+
- 4<
- a% = -2
- '"Unable to claim enough memory"
- 5>
- a% = -1
- '"Block size is now 0 or negative"
-
-
- quit% =
- quit%
- -- RMA Heap Procedures --------------------------------------------
- Global variables used:
- None
- _heap_get(size%)
- ptr%,heap%,flags%
- Returns pointer to new memory block unless claim fails
- in which case -1 is returned
- First find start address of RMA..
- "OS_ReadDynamicArea",1
- heap%
- Now claim memory, trapping errors by using X form of SWI..
- "XOS_Module",6,,,size%
- ,,ptr%;flags%
- If error occured return -1, else return address of allocated
- block
- (flags%
- ptr% = -1
- = ptr%
- _heap_release(
- ptr%)
- maxfree%,nrpages%,flags%
- Returns 0 if block released OK
- Returns -1 if operation failed (i.e. block doesn't exist)
- "XOS_Module",7,,ptr%
- ;flags%:
- Free the block
- (flags%
- 1) = 0
- Block was released successfully...
- ptr% = 0
- Error occured trying to free the block, return -1 to signal to the
- program that something went wrong (normally the program would ignore
- this anyway)
- ptr% = -1
- _heap_resize(
- ptr%,change%)
- flags%,heap%
- Returns a new pointer to the block (it may be moved in memory). Any data
- in the block will be copied to the new location if necessary.
- Returns -1 if the block now has a size of 0 or less
- Returns -2 if claim fails due to lack of memory
- Returns -3 if block does not exist
- First find start address of RMA..
- "OS_ReadDynamicArea",1
- heap%
- Read size of block to check it exists (not strictly neccessary, but it
- does enable us to give more meaningful error indications)
- "XOS_Heap",6,heap%,ptr%
- ;flags%
- (flags%
- It doesn't exist..
- ptr% = -3
- It does, so attempt to perform resize..
- "XOS_Heap",4,heap%,ptr%,change%
- ,,ptr%;flags%
- (flags%
- ptr% = -2
-